package com.hadoop.project.utils;

import com.hadoop.project.entity.hbase.*;
import com.hadoop.project.entity.po.TabRank;
import com.hadoop.project.entity.po.UserFlavor;
import com.hadoop.project.entity.po.VideoRank;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Count {
    public static ProcessRes process(HBaseResult hBaseResult) {
        ProcessRes result =new ProcessRes();

        // 初始化map
        Map<Long,Long> videoCountMap=new HashMap<>();
        Map<String,Long> tabCountMap=new HashMap<>();
        Map<String,String> tabMap=new HashMap<>();
        // 用户名, 用户喜好标签，用户观看次数
        Map<String,Map<String,Long>> userCountMap=new HashMap<>();
        for(Video video:hBaseResult.getVideoList()){
            if(video.getVid()!=null) videoCountMap.put(Long.valueOf(video.getVid()),0L);
        }
        for (Tab tab:hBaseResult.getTabList()){
            if(tab.getTid() != null) {
                tabCountMap.put(tab.getTid().replace("tid-", ""),0L);
                tabMap.put(tab.getTid().replace("tid-", ""),tab.getTname());
            }

        }


        //计算次数
        Map<String,String> videotabsMap=new HashMap<>();
        for(Video video:hBaseResult.getVideoList()){
            if(video.getVid()==null) continue;
            videotabsMap.put(video.getVid(),video.getTabs());
            String[] tids = video.getTabs().split(",");
            for (String tid:tids){
                String t = tid.replace("tid-", "");
                if(tabCountMap.get(t)!=null){
                    tabCountMap.put(t,tabCountMap.get(t)+1);
                }
            }
        }
        for(UserLog userLog:hBaseResult.getUserLogList()){
            if (userLog.getVid()==null) continue;
            // 观看时间是否超过3秒
            if(Integer.parseInt(userLog.getVideoTime())<=3){
                continue;
            }
            //vid是否在videoCountMap内
            if(videoCountMap.get(Long.valueOf(userLog.getVid()))!=null){
                videoCountMap.put(Long.valueOf(userLog.getVid()),videoCountMap.get(Long.valueOf(userLog.getVid()))+1);
            }
            String uid=userLog.getUid().replace("uid-","");
            if(userCountMap.get(uid)==null){
                Map<String,Long> usertabmap=new HashMap<>();
                String [] tids = videotabsMap.get(userLog.getVid()).split(",");
                for(String tid:tids){
                    usertabmap.put(tid.replace("tid-",""),1L);
                }
                userCountMap.put(uid,usertabmap);
            }else{
                String [] tids = videotabsMap.get(userLog.getVid()).split(",");
                for(String tid:tids){
                    String ret = tid.replace("tid-", "");
                    if(userCountMap.get(uid).get(ret)==null){
                        userCountMap.get(uid).put(ret,1L);
                    }
                    else {
                        userCountMap.get(uid).put(ret,userCountMap.get(uid).get(ret)+1);
                    }
                }
            }
        }
        //计算用户喜好标签总数
        Map<String,Double> userTotalCountMap=new HashMap<>();
        for(String uid:userCountMap.keySet()){
            Double sum=0.0;
            for(String tid:userCountMap.get(uid).keySet()){
                sum+=userCountMap.get(uid).get(tid);
            }
            userTotalCountMap.put(uid,sum);
        }



        //  结果注入
        List<VideoRank> videoRankList = new ArrayList<>();
        List<TabRank> tabRankList = new ArrayList<>();
        List<UserFlavor> userFlavorList = new ArrayList<>();
        long i=1L;
        for(Long vid:videoCountMap.keySet()){
            videoRankList.add(VideoRank.builder().id(i++).vid(vid).views(videoCountMap.get(vid)).build());
        }
        i=1L;
        for(String tid:tabCountMap.keySet()){
            tabRankList.add(TabRank.builder().id(i++).tname(tabMap.get(tid)).cnt(tabCountMap.get(tid)).build());
        }
        i=1L;
        for (String uid:userCountMap.keySet()){
            for(String tid:userCountMap.get(uid).keySet()){
                String tpercent=String.format("%.4f",userCountMap.get(uid).get(tid)/userTotalCountMap.get(uid));
                userFlavorList.add(UserFlavor.builder().id(i++).uid(Long.valueOf(uid)).tname(tabMap.get(tid)).tpercent(tpercent).build());
            }
        }



        result.setVideoRankList(videoRankList);
        result.setTabRankList(tabRankList);
        result.setUserFlavorList(userFlavorList);
        return result;
    }

    public static void main(String[] args) {
        Count count = new Count();
    }
}
